import numpy as np
import cv2

red_lower = np.array([156,43,46])
red_upper = np.array([180,255,255])
green_lower = np.array([35,43,46])
green_upper = np.array([77,255,255])
blue_lower = np.array([100,43,46])
blue_upper = np.array([124,255,255])

cap = cv2.VideoCapture(0)

cap.set(3, 320)
cap.set(4, 240)

while 1:
    ret, frame = cap.read()
    frame = cv2.GaussianBlur(frame, (5, 5), 0)
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    red_mask = cv2.inRange(hsv, red_lower, red_upper)
    red_mask = cv2.erode(red_mask,None, iterations=2)
    red_mask = cv2.GaussianBlur(red_mask, (3, 3), 0)

    green_mask = cv2.inRange(hsv, green_lower, green_upper)
    green_mask = cv2.erode(green_mask, None, iterations=2)
    green_mask = cv2.GaussianBlur(green_mask, (3, 3), 0)

    blue_mask = cv2.inRange(hsv, blue_lower, blue_upper)
    blue_mask = cv2.erode(blue_mask, None, iterations=2)
    blue_mask = cv2.GaussianBlur(blue_mask, (3, 3), 0)

    #res = cv2.bitwise_and(frame, frame, mask=mask)

    red_cnts = cv2.findContours(red_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
    green_cnts = cv2.findContours(green_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
    blue_cnts = cv2.findContours(blue_mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]

    k=np.size(red_cnts)
    z=len(red_cnts)
    print(int(k))
    print(int(z))
    if len(red_cnts) > 0:
        cnt = max(red_cnts, key=cv2.contourArea)
        (x, y), radius = cv2.minEnclosingCircle(cnt)
        cv2.circle(frame, (int(x), int(y)), int(radius), (0, 0, 0), 2)
        if radius > 40:#240/6=40
            cv2.putText(frame, "X:" + str(x)+" Y:"+str(y), (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0))
        #print("Red X:",int(x)," Y:",int(y))
    #else:
    #    pass
    if len(green_cnts) > 0:
        cnt = max(green_cnts, key=cv2.contourArea)
        (x, y), radius = cv2.minEnclosingCircle(cnt)
        cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2)
        cv2.putText(frame, "X:" + str(x)+" Y:"+str(y), (int(x), int(y)),cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255))
        #cv2.putText(frame,"Green X:",int(x)," Y:",int(y),(x,y),cv2.FONT_HERSHEY_SCRIPT_COMPLEX,1.0,(0,255,255),2)
        #print("Green X:",int(x)," Y:",int(y))
    #else:
    #   pass

    if len(blue_cnts) > 0:
        cnt = max(blue_cnts, key=cv2.contourArea)
        (x, y), radius = cv2.minEnclosingCircle(cnt)

        cv2.circle(frame, (int(x), int(y)), int(radius), (255, 0, 255), 2)
        if radius > 40:#240/6=40
            cv2.putText(frame, "X:" + str(x)+" Y:"+str(y), (int(x), int(y)), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0))
        #cv2.putText(frame,"Blue X:",int(x)," Y:",int(y),(x,y),cv2.FONT_HERSHEY_SCRIPT_COMPLEX,1.0,(255,0,255),2)
        #print("Blue X:",int(x)," Y:",int(y))
    #else:
    #   pass
    cv2.imshow('frame', frame)
#cv2.imshow('mask', mask)
#cv2.imshow('res', res)
    if cv2.waitKey(5) & 0xFF == 27:
        break
cap.release()
cv2.destroyAllWindows()






